/* Daniel Shiffman               */
/* Bayesian Spam Filter Example  */
/* Programming from A to Z       */
/* Spring 2006                   */
/* http://www.shiffman.net       */
/* daniel.shiffman@nyu.edu       */
// Main driver program
package bayes;

import analisador.MiniMundoBayes;
import util.A2ZFileReader;
import java.io.*;

public class Bayesian {

    public static void main(String args[]) throws Exception {
        int acertouContribuir = 0, acertouNaoContribuir = 0;
        int deveriaSerContribuir = 0, deveriaSerNaoContribuir = 0;
        try {
            
            MiniMundoBayes.gerarTreino();
            
            // Create a new SpamFilter Object
            SpamFilter filter = new SpamFilter();

            // Train spam with a file of spam e-mails
            filter.trainSpam("Eli.Bad.txt");
            // Train spam with a file of regular e-mails
            filter.trainGood("Eli.Good.txt");
            // We are finished adding words so finalize the results
            filter.finalizeTraining();

            File dir = new File("bayes/");
            String arquivos[] = dir.list();
            for (String arquivo : arquivos) {

                // Read in a text file
                A2ZFileReader fr = new A2ZFileReader("bayes/" + arquivo);
                String stuff = fr.getContent();

                // Ask the filter to analyze it
                float spam = filter.analyze(stuff);

                String t = arquivo.substring(0, 1);


                //Não deve contribuir
                if (t.equals("0")) {
                    if (spam <= 0.9f) { //Não é spam
                        deveriaSerNaoContribuir++;
                    } else {
                        acertouNaoContribuir++;
                    }
                } //Deve contribuir
                else {
                    if (spam <= 0.9f) {
                        acertouContribuir++; 
                    } else {
                        
                        deveriaSerContribuir++;
                    }
                }

                System.out.println(arquivo + ":" + (spam < 0.9f) + "-" + spam);

            }
            double a = acertouContribuir;
            double b = deveriaSerContribuir;
            double c = deveriaSerNaoContribuir;
            double d = acertouNaoContribuir;
            System.out.println("a:"+a);
            System.out.println("b:"+b);
            System.out.println("c:"+c);
            System.out.println("d:"+d);
            System.out.println("Taxa de erro do contribuir:" + (b / (a + b)));
            System.out.println("Taxa de erro do Não Contribuir:" + (c / (c + d)));
            System.out.println("Taxa de erro do Overall:" + ((b + c) / (a + b + c + d)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
